package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.NoSaleReportPresenter;
import com.emser.unc.serializable.NoSaleReportDTO;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.Range;

public class NoSaleReportView extends Composite implements NoSaleReportPresenter.Display {

	private VerticalPanel verticalPanel;
	private Button btnRemove;
	private MultiSelectionModel<NoSaleReportDTO> selectionModel = new MultiSelectionModel<NoSaleReportDTO>();
	private CellTable<NoSaleReportDTO> saleReportsTable;
	private SimplePager pager;
	private int pageSize = CloudMobile.constants.pageSizeCP();
	private BusyIndicatorView busyIndicator =  new BusyIndicatorView(CloudMobile.constants.msjWorking());
	private MessageWView messageDelete;
	private int heightSPanel;
	public NoSaleReportView() {
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		heightSPanel = CloudMobile.get().getHeightPanelCenter() - 100;
		initHPanelTitle();
		ScrollPanel sPanel = new ScrollPanel();
		sPanel.setSize("100%", heightSPanel+"px");
		saleReportsTable= new CellTable<NoSaleReportDTO>();
	    initColumnsTable();
	    saleReportsTable.setSelectionModel(selectionModel, DefaultSelectionEventManager.<NoSaleReportDTO>createCheckboxManager());
	    saleReportsTable.setSize("100%", "100%");
	    sPanel.setWidget(saleReportsTable);
	    verticalPanel.add(sPanel);
	    verticalPanel.setCellHorizontalAlignment(sPanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(sPanel, "100%");
	    verticalPanel.setCellHeight(sPanel, heightSPanel+"px");
	    initPager();
	    messageDelete = new MessageWView();
	    messageDelete.getLblMessageW().setText(CloudMobile.constants.msjWarningDelete());
	    messageDelete.hide();
	    busyIndicator.hide();
	}

	private void initPager() {
		SimplePanel simplePanel = new SimplePanel();
	    verticalPanel.add(simplePanel);
	    verticalPanel.setCellVerticalAlignment(simplePanel, HasVerticalAlignment.ALIGN_MIDDLE);
	    simplePanel.setSize("400px", "40px");
	    verticalPanel.setCellHorizontalAlignment(simplePanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(simplePanel, "100%");
	    verticalPanel.setCellHeight(simplePanel, "40px");
	    pager = new SimplePager(TextLocation.CENTER){
	    	@Override
	    	public void setPageStart(int index) {
	    	  if (getDisplay() != null) {
	    	    Range range = getDisplay().getVisibleRange();
	    	    int pageSize = range.getLength();
	    	    index = Math.max(0, index);
	    	    if (index != range.getStart()) {
	    	      getDisplay().setVisibleRange(index, pageSize);
	    	    }
	    	  }
	    	}
	    };
	    pager.setStyleName("gwt-pager1");
	    pager.setStylePrimaryName("gwt-pager1");
	    pager.setRangeLimited(true);
	    pager.setDisplay(saleReportsTable);
	    pager.setSize("400px", "15px");
	    pager.setPageSize(pageSize);
	    pager.setPage(0);
	    simplePanel.add(pager);
	}

	private void initColumnsTable() {
		Column<NoSaleReportDTO, Boolean> checkColumn = new Column<NoSaleReportDTO, Boolean>(
	    	    new CheckboxCell(true, false))
	    	        {
	    	            @Override
	    	            public Boolean getValue(NoSaleReportDTO t)
	    	            {
	    	                return selectionModel.isSelected(t);
	    	            }
	    	        };
		checkColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		saleReportsTable.addColumn(checkColumn, "");
		TextColumn<NoSaleReportDTO> idColumn = new TextColumn<NoSaleReportDTO>() {
		      @Override
		      public String getValue(NoSaleReportDTO object) {
		        return object.getIdReport().toString();
		      }
		    };
		idColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		saleReportsTable.addColumn(idColumn, CloudMobile.constants.code());
	    TextColumn<NoSaleReportDTO> dateColumn = new TextColumn<NoSaleReportDTO>() {
	      @Override
	      public String getValue(NoSaleReportDTO object) {
	        return DateTimeFormat.getFormat( DateTimeFormat.PredefinedFormat.DATE_TIME_MEDIUM).format(object.getDate());
	      }
	    };
	    dateColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    saleReportsTable.addColumn(dateColumn, CloudMobile.constants.date());  
	    TextColumn<NoSaleReportDTO> sellerColumn = new TextColumn<NoSaleReportDTO>() {
	      @Override
	      public String getValue(NoSaleReportDTO object) {
	        return object.getSellerUserName();
	      }
	    };
	    sellerColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    saleReportsTable.addColumn(sellerColumn, CloudMobile.constants.seller());
	    
	    TextColumn<NoSaleReportDTO> customerColumn = new TextColumn<NoSaleReportDTO>() {
		      @Override
		      public String getValue(NoSaleReportDTO object) {
		        return object.getCustomerID().toString();
		      }
		    };
		customerColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		saleReportsTable.addColumn(customerColumn, CloudMobile.constants.customer());	
		TextColumn<NoSaleReportDTO> reasonColumn = new TextColumn<NoSaleReportDTO>() {
		      @Override
		      public String getValue(NoSaleReportDTO object) {
		        return object.getReason();
		      }
		    };
		reasonColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		saleReportsTable.addColumn(reasonColumn, CloudMobile.constants.reason());
	}

	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-SimplePanelM1");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setSize("100%", "40px");
	    
	    btnRemove = new Button(CloudMobile.constants.remove());
	    horizontalPanel.add(btnRemove);
	    horizontalPanel.setCellHeight(btnRemove, "28");
	    horizontalPanel.setCellWidth(btnRemove, "120");
	    btnRemove.setSize("112", "28");
	    horizontalPanel.setCellVerticalAlignment(btnRemove, HasVerticalAlignment.ALIGN_MIDDLE);
	    verticalPanel.add(horizontalPanel);  
	    verticalPanel.setCellHeight(horizontalPanel, "40px");
	    verticalPanel.setCellWidth(horizontalPanel, "100%");
	    Label lblNewLabel = new Label(CloudMobile.constants.saleNoReports());
	    horizontalPanel.add(lblNewLabel);
	    horizontalPanel.setCellVerticalAlignment(lblNewLabel, HasVerticalAlignment.ALIGN_MIDDLE);
	    lblNewLabel.setSize("100%", "50%");
	    horizontalPanel.setCellHeight(lblNewLabel, "100%");
	    horizontalPanel.setCellWidth(lblNewLabel, "200px");
	}

	@Override
	public HasClickHandlers getRemoveButton() {
		return btnRemove;
	}

	@Override
	public MultiSelectionModel<NoSaleReportDTO> getSelectionModel() {
		return selectionModel;
	}

	@Override
	public HasData<NoSaleReportDTO> getTable() {
		return saleReportsTable;
	}
	
	@Override
	public void enableDelete(boolean enabled) {
		btnRemove.setEnabled(enabled);	
	}

	@Override
	public void setPage(int page) {
		pager.setPage(page);	
	}

	@Override
	public int getPage() {
		return pager.getPage();
	}

	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}

	@Override
	public void showWarning() {
		messageDelete.show();
		
	}

	@Override
	public void disableWarning() {
		messageDelete.hide();
		
	}

	@Override
	public void enableBtnPopUpDelete(boolean enabled) {
		messageDelete.getBtnDelete().setEnabled(enabled);
		
	}

	@Override
	public HasClickHandlers getBtnPopUpDCancel() {
		return messageDelete.getBtnCancel();
	}

	@Override
	public HasClickHandlers getBtnPopUpDDelete() {
		return messageDelete.getBtnDelete();
	}

}
